# Contributor: Steeve Chailloux <steeve@chaahk.com>
# Maintainer: Stuart Cardall <developer@it-offshore.co.uk>
pkgname=logstash
pkgver=7.5.2
pkgrel=0
pkgdesc="A flexible, open source, data collection, parsing and enrichment pipeline"
url="http://logstash.net"
arch="noarch !armhf !armv7 !mips !mips64 !x86 !ppc64le" # java-jffi missing
license="Apache-2.0"
depends="openjdk8-jre-base java-jffi-native libc6-compat bash"
makedepends="$depends_dev"
install="$pkgname.pre-install"
pkgusers="logstash"
subpackages="$pkgname-doc $pkgname-openrc"
options="!strip !check"
source="$pkgname-$pkgver.tar.gz::https://artifacts.elastic.co/downloads/logstash/logstash-$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd
	$pkgname.conf
	$pkgname.profile
	"

package() {
	local datadir=/var/lib/logstash logdir=$datadir/logs confdir=/etc/logstash
	local homedir=/usr/share/logstash heapsize=150m

	# create the necessary dirs
	install -dm755 "$pkgdir/$homedir"
	install -dm755 "$pkgdir/$logdir"
	install -dm755 "$pkgdir/$datadir"
	install -dm755 "$pkgdir/$confdir"
	install -dm755 "$pkgdir/$confdir/conf.d"

	# install scripts
	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.conf \
		"$pkgdir/$confdir"/conf.d/
	install -m644 -D "$srcdir"/$pkgname.profile \
		"$pkgdir"/etc/profile.d/logstash.sh

	# copy files
	cp -r * "$pkgdir/$homedir/"
	cp config/* "$pkgdir/$confdir/"

	# move docs
	mkdir -p "$pkgdir"/usr/share/doc/logstash && cd "$pkgdir"
	find usr/share/logstash -maxdepth 1 -type f ! -name Gemfile* \
		-name "[[:upper:]]*" -exec mv {} usr/share/doc/logstash/ \;

	# remove files for windows
	find usr/share/logstash -type f -name *.bat -exec rm -f {} \;
	find usr/share/logstash -type f -name *win32* -exec rm -f {} \;
	find usr/share/logstash -type f -name *Win32* -exec rm -f {} \;

	# remove bundled libjffi (we use Alpine's java-jffi-native)
	rm -rf "$pkgdir"/usr/share/logstash/vendor/jruby/lib/jni

	# set default paths
	sed -i \
	  -e "s|# path.config:|path.config: $confdir/conf.d|" \
	  -e "s|# path.logs:|path.logs: $logdir|" \
	  -e "s|# path.data:|path.data: $datadir|" \
	"$pkgdir/$confdir"/logstash.yml

	# symlink so manual commands without -path.settings works
	rmdir "$pkgdir/$homedir"/data
	ln -s "$datadir" "$pkgdir/$homedir"/data
	ln -s "$logdir" "$pkgdir/$homedir"/logs

	# reduce heap sizes
	sed -i \
	  -e "s|^-Xms.*|-Xms$heapsize|" \
	  -e "s|^-Xmx.*|-Xmx$heapsize|" \
	"$pkgdir/$confdir"/jvm.options
}

sha512sums="955e546372466fb557eb15b9f466decef24c520c7ff16afd5432d5a8d1dd1f061f11fb9178678bed74b3c16a2d5ee6cd72fc8e4d757c8af36747dc25522c443e  logstash-7.5.2.tar.gz
94b18da3f0ef40f68118b27654563e7aa6dd4a3b90f0d8ad61a8be579d4be62c2bf1b192a32ee6140b81224343eeb442687539f6def1e725787b8cb96c0c70d1  logstash.confd
828684ea0b9faa53ac51f7fb22bf73d862dc058d605f27958db6a8efbd10f5463a0837fa88b62a3964582c72fdd7ece434057a540b0924eae1fae17b9d5887b4  logstash.initd
21609c8f44b31a5f298573be4b5c0fdcdc165e603a95c3626570fb6dd44dad114a74ee3f10fbd518f72b537a3d6bdcdbad09798c4bead0ee16f2a3b8e2b00ed3  logstash.conf
180349d57a6a48be9052649425a945a07cf5e194d0f32f83567b460936729a70244d73c3495f8a318a3b98ac99a2848cadfbee5bd017da803c68c5140b6cd29d  logstash.profile"
